import os
import pyautogui
from dataclasses import dataclass
import cv2
import time  # 导入time模块
import numpy as np
from PIL import ImageGrab
from datetime import datetime
import mjtools
import random
import pandas as pd
import os
from openpyxl import load_workbook  # 用于读取现有工作表的行数

screen_width, screen_height = pyautogui.size()


# 创建结构体Button
@dataclass
class AreaPosition:
    name: str
    left: int
    right: int
    top: int
    bottom: int


my_turn_position = AreaPosition("我的回合", screen_width / 2 - 45, screen_width / 2 + 45, screen_height / 2 - 39,
                                screen_height / 2 - 29)
next_game_button_position = AreaPosition("再来一场", screen_width - 585, screen_width - 335, screen_height - 125,
                                         screen_height - 55)
next_game_confirm_button_position = AreaPosition("再来一场确认按钮", 630, screen_width - 990,
                                                 screen_height - 325,
                                                 screen_height - 255)
get_reward_position = AreaPosition("获得奖励", 830,
                                   1100,
                                   220,
                                   280)
ranking_position = AreaPosition("段位场", screen_width - 800, screen_width - 300, 250,
                                400)
silver_room_position = AreaPosition("银之间", screen_width - 770, screen_width - 300, 520,
                                    650)
siren_dong_position = AreaPosition("银之间", screen_width - 770, screen_width - 300, 340,
                                   450)
confirm_button_position = AreaPosition("小局确认", screen_width - 190, screen_width - 100, screen_height - 110,
                                       screen_height - 55)
end_confirm_button_position = AreaPosition("终局确认", screen_width - 300, screen_width - 50, screen_height - 125,
                                           screen_height - 55)
first_button_position = AreaPosition("按钮一", screen_width - 700, screen_width - 530, screen_height - 270,
                                     screen_height - 217)
second_button_position = AreaPosition("按钮二", screen_width - 990, screen_width - 820, screen_height - 270,
                                      screen_height - 217)
third_button_position = AreaPosition("按钮三", screen_width - 1280, screen_width - 1110, screen_height - 270,
                                     screen_height - 217)
he_button_position = AreaPosition("和", 15, 65, 590, 640)
ming_button_position = AreaPosition("鸣", 15, 65, 653, 703)
qie_button_position = AreaPosition("切", 15, 65, 716, 766)
card_top = screen_height - 160
card_bottom = screen_height - 10
card_position = [
    AreaPosition("35", 220, 221, 0, 1),
    AreaPosition("35", 221, 316, card_top, card_bottom),
    AreaPosition("35", 317, 411, card_top, card_bottom),
    AreaPosition("35", 411, 506, card_top, card_bottom),
    AreaPosition("35", 506, 601, card_top, card_bottom),
    AreaPosition("35", 601, 696, card_top, card_bottom),
    AreaPosition("35", 695, 790, card_top, card_bottom),
    AreaPosition("35", 790, 885, card_top, card_bottom),
    AreaPosition("35", 885, 980, card_top, card_bottom),
    AreaPosition("35", 980, 1075, card_top, card_bottom),
    AreaPosition("35", 1075, 1170, card_top, card_bottom),
    AreaPosition("35", 1169, 1264, card_top, card_bottom),
    AreaPosition("35", 1264, 1359, card_top, card_bottom),
    AreaPosition("35", 1359, 1454, card_top, card_bottom),
    AreaPosition("35", 1485, 1580, card_top, card_bottom),
]
card_available_position = [
    AreaPosition("35", 220, 221, 0, 1),
    AreaPosition("35", 231, 238, card_top + 20, card_top + 27),
    AreaPosition("35", 327, 334, card_top + 20, card_top + 27),
    AreaPosition("35", 421, 428, card_top + 20, card_top + 27),
    AreaPosition("35", 516, 523, card_top + 20, card_top + 27),
    AreaPosition("35", 611, 618, card_top + 20, card_top + 27),
    AreaPosition("35", 705, 712, card_top + 20, card_top + 27),
    AreaPosition("35", 800, 807, card_top + 20, card_top + 27),
    AreaPosition("35", 895, 902, card_top + 20, card_top + 27),
    AreaPosition("35", 990, 997, card_top + 20, card_top + 27),
    AreaPosition("35", 1085, 1092, card_top + 20, card_top + 27),
    AreaPosition("35", 1179, 1186, card_top + 20, card_top + 27),
    AreaPosition("35", 1274, 1281, card_top + 20, card_top + 27),
    AreaPosition("35", 1369, 1376, card_top + 20, card_top + 27),
    AreaPosition("35", 1495, 1502, card_top + 20, card_top + 27),
]
free_area = AreaPosition("自由点击区域", 500, screen_width - 500, 200, screen_height - 400)


# 把当前的手牌记录到excel中，添加决策树的训练集
def write_cards_to_excel():
    # 1. 构造数据行（注意：键的顺序建议和最终Excel表头一致，避免列错乱）
    data_row = {
        "牌01": card_position[1].name,  # 建议按顺序排列，避免Excel列顺序混乱
        "牌02": card_position[2].name,
        "牌03": card_position[3].name,
        "牌04": card_position[4].name,
        "牌05": card_position[5].name,
        "牌06": card_position[6].name,
        "牌07": card_position[7].name,
        "牌08": card_position[8].name,
        "牌09": card_position[9].name,
        "牌10": card_position[10].name,
        "牌11": card_position[11].name,
        "牌12": card_position[12].name,
        "牌13": card_position[13].name,
        "牌14": card_position[14].name,
        "丢弃的牌": card_position[0].name
    }

    # 2. 转换为DataFrame
    df = pd.DataFrame([data_row])

    # 3. 写入Excel（关键：计算追加位置）
    file_path = "mahjong_data.xlsx"
    sheet_name = "Sheet1"  # 明确工作表名称（默认是Sheet1）

    if not os.path.exists(file_path):
        # 首次创建：直接写入（含表头）
        df.to_excel(file_path, index=False, sheet_name=sheet_name)
        print("文件创建成功，写入表头和首行数据")
    else:
        # 追加数据：先获取现有行数，从最后一行+1开始写
        # 加载现有工作表，获取最大行数
        wb = load_workbook(file_path)
        ws = wb[sheet_name]
        startrow = ws.max_row  # 已有数据的最后一行（表头+内容）
        wb.close()  # 关闭工作簿，避免占用

        # 追加写入
        with pd.ExcelWriter(
                file_path,
                mode='a',
                engine='openpyxl',
                if_sheet_exists='overlay'
        ) as writer:
            # 明确从startrow开始写入，header=False不写表头
            df.to_excel(writer, index=False, header=False, sheet_name=sheet_name, startrow=startrow)

        # print(f"数据追加成功，写入位置：第{startrow + 1}行")


# 判断当前全部牌是什么
def get_cards():
    img_num = 1
    for pos in range(1, 14):
        # 截取屏幕（会自动保存）
        screen_img = mjtools.capture(card_position[pos])
        while img_num < 35:
            reference_path = f"referenceImg/{img_num}.jpg"
            if not os.path.exists(reference_path):
                print(f"错误：同目录下未找到 referenceImg/{img_num}.jpg文件")
                break
            # 加载参考图片
            reference_img = cv2.imread(reference_path)
            similarity = mjtools.compare_image_in_larger_image(reference_img, screen_img)
            # print(f"图片相似度: {similarity:.2f}%")
            if similarity > 80:
                # print(f"牌{pos}匹配到: {img_num}")
                card_position[pos].name = img_num
                break
            img_num = img_num + 1

    # 处理第14张也就是新牌
    img_num = 1
    screen_img = mjtools.capture(card_position[14])
    while img_num < 35:
        reference_path = f"referenceImg/{img_num}.jpg"
        if not os.path.exists(reference_path):
            print(f"错误：同目录下未找到 referenceImg/{img_num}.jpg文件")
            break
        # 加载参考图片
        reference_img = cv2.imread(reference_path)
        similarity = mjtools.compare_image_in_larger_image(reference_img, screen_img)
        # print(f"图片相似度: {similarity:.2f}%")
        if similarity > 75:
            card_position[14].name = img_num
            # print(f"牌{14}匹配到: {img_num}")
            break
        img_num = img_num + 1

    for pos in range(1, 15):
        print(f"card_position[{pos}] 的值为：{card_position[pos].name}")


# 找到第一个可以用于和牌的牌，从右到左
def find_first_card_available():
    # 检查参考图片是否存在
    reference_path = "referenceImg/card_available.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到 card_available.jpg文件")
        return
    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    for pos in range(13, 0, -1):

        # 截取屏幕（会自动保存）
        screen_img = mjtools.capture(card_available_position[pos])

        # 比较相似度
        similarity = mjtools.compare_images_with_color(screen_img, reference_img)
        print(f"可用于立直的牌图片相似度: {similarity:.2f}%")
        if similarity >= 96:
            return pos

    return 0


# 点击指定位置的牌
def move_to_and_click_card_at_pos(pos: int):
    x = (card_position[pos].left + card_position[pos].right) / 2
    y = (card_position[pos].top + card_position[pos].bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击牌面为输入值的牌
def move_to_and_click_card(card_img_bum: int):
    for pos in range(14, 0, -1):
        # 从后往前找需要选择的花色的卡牌
        if card_position[pos].name == card_img_bum:
            x = (card_position[pos].left + card_position[pos].right) / 2
            y = (card_position[pos].top + card_position[pos].bottom) / 2
            mjtools.move_to_and_click(x, y)
            break


# 点击随机牌
def move_to_and_click_random_card():
    pos = random.randint(1, 14)
    move_to_and_click_card_at_pos(pos)


# def check_area(area: AreaPosition):


# 检查是否为自己的回合
def check_my_turn():
    # 检查参考图片是否存在
    reference_path = "referenceImg/my_turn.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到my_turn.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img1 = mjtools.capture(my_turn_position)
    time.sleep(0.5)  # 延时半秒钟
    screen_img2 = mjtools.capture(my_turn_position)

    # 比较相似度
    similarity1 = mjtools.compare_images_with_color(screen_img1, reference_img)
    similarity2 = mjtools.compare_images_with_color(screen_img2, reference_img)
    # print(f"自己回合图片相似度1: {similarity1:.2f}%")
    # print(f"自己回合图片相似度2: {similarity2:.2f}%")
    if similarity1 >= 90 or similarity2 >= 90:
        return True
    else:
        return False


# 检查是否有排位场按钮
def check_ranking():
    # 检查参考图片是否存在
    reference_path = "referenceImg/ranking.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到ranking.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(ranking_position)

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    print(f"排位场图片相似度: {similarity:.2f}%")
    if similarity >= 80:
        return True
    else:
        return False


# 检查是否有银之间按钮
def check_silver_room():
    # 检查参考图片是否存在
    reference_path = "referenceImg/silver_room.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到 silver_room.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(silver_room_position)

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    print(f"银之间图片相似度: {similarity:.2f}%")
    if similarity >= 80:
        return True
    else:
        return False


# 检查是否有 四人东 按钮
def check_siren_dong():
    # 检查参考图片是否存在
    reference_path = "referenceImg/siren_dong.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到 siren_dong.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(siren_dong_position)

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    print(f"四人东 图片相似度: {similarity:.2f}%")
    if similarity >= 96:
        return True
    else:
        return False


# 检查是否有再来一场按钮
def check_next_game():
    # 检查参考图片是否存在
    reference_path = "referenceImg/NextGame.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到再来一场.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(next_game_button_position)

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    print(f"再来一场图片相似度: {similarity:.2f}%")
    if similarity >= 70:
        return True
    else:
        return False


# 检查是否有再来一场的确认按钮
def check_next_game_confirm():
    # 检查参考图片是否存在
    reference_path = "referenceImg/next_game_confirm.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到再来一场的确认按钮.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(next_game_confirm_button_position)

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    # print(f"再来一场的确认按钮图片相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有小局结束确认按钮
def check_confirm():
    # 检查参考图片是否存在
    reference_path = "referenceImg/confirm.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到小局结束确认按钮.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(confirm_button_position)

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    print(f"小局结束确认图片相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有获取奖励按钮
def check_get_reward():
    # 检查参考图片是否存在
    reference_path = "referenceImg/get_reward.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到获取奖励.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(get_reward_position)

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    print(f"获取奖励图片相似度: {similarity:.2f}%")
    if similarity >= 70:
        return True
    else:
        return False


# 检查是否有终局结束确认按钮
def check_end_confirm():
    # 检查参考图片是否存在
    reference_path = "referenceImg/end_confirm.jpg"
    if not os.path.exists(reference_path):
        # print("错误：同目录下未找到终局结束确认按钮.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(end_confirm_button_position)

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    print(f"终局结束确认图片相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有立直按钮
def check_li_zhi(pos: int):
    # 检查参考图片是否存在
    reference_path = "referenceImg/lizhi_2.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到立直按钮.jpg文件")
        return False

    # 加载参考图片
    reference_img = cv2.imread(reference_path)
    # 截取屏幕（会自动保存）
    if pos == 1:
        screen_img = mjtools.capture(first_button_position)
    elif pos == 2:
        screen_img = mjtools.capture(second_button_position)
    elif pos == 3:
        screen_img = mjtools.capture(third_button_position)
    else:
        return False

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    # print(f"立直图片相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有自摸按钮
def check_zi_mo(pos: int):
    # 检查参考图片是否存在
    reference_path = "referenceImg/zimo_2.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到自摸按钮.jpg文件")
        return False

    # 加载参考图片
    reference_img = cv2.imread(reference_path)
    # 截取屏幕（会自动保存）
    if pos == 1:
        screen_img = mjtools.capture(first_button_position)
    elif pos == 2:
        screen_img = mjtools.capture(second_button_position)
    elif pos == 3:
        screen_img = mjtools.capture(third_button_position)
    else:
        return False

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    # print(f"自摸图片相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有和按钮
def check_he(pos: int):
    # 检查参考图片是否存在
    reference_path = "referenceImg/he_2.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到和按钮.jpg文件")
        return False

    # 加载参考图片
    reference_img = cv2.imread(reference_path)
    # 截取屏幕（会自动保存）
    if pos == 1:
        screen_img = mjtools.capture(first_button_position)
    elif pos == 2:
        screen_img = mjtools.capture(second_button_position)
    elif pos == 3:
        screen_img = mjtools.capture(third_button_position)
    else:
        return False

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    # print(f"和图片相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有跳过按钮
def check_skip():
    # 检查参考图片是否存在
    reference_path = "referenceImg/skip.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到跳过按钮.jpg文件")
        return False

    # 加载参考图片
    reference_img = cv2.imread(reference_path)
    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(first_button_position)

    # 比较相似度
    similarity = mjtools.compare_image_in_larger_image(screen_img, reference_img)
    # print(f"跳过图片相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有和on按钮
def check_he_on():
    # 检查参考图片是否存在
    reference_path = "referenceImg/he_on.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到和on.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(he_button_position)

    # 比较相似度
    similarity = mjtools.compare_images_with_color(screen_img, reference_img)
    # print(f"和on图片相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有和off按钮
def check_he_off():
    # 检查参考图片是否存在
    reference_path = "referenceImg/he_off.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到和off.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(he_button_position)

    # 比较相似度
    similarity = mjtools.compare_images_with_color(screen_img, reference_img)
    # print(f"和off相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有鸣on按钮
def check_ming_on():
    # 检查参考图片是否存在
    reference_path = "referenceImg/ming_on.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到鸣on.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(ming_button_position)

    # 比较相似度
    similarity = mjtools.compare_images_with_color(screen_img, reference_img)
    # print(f"鸣on相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有鸣off按钮
def check_ming_off():
    # 检查参考图片是否存在
    reference_path = "referenceImg/ming_off.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到鸣off.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(ming_button_position)

    # 比较相似度
    similarity = mjtools.compare_images_with_color(screen_img, reference_img)
    # print(f"鸣off相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有切on按钮
def check_qie_on():
    # 检查参考图片是否存在
    reference_path = "referenceImg/qie_on.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到切on.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(qie_button_position)

    # 比较相似度
    similarity = mjtools.compare_images_with_color(screen_img, reference_img)
    # print(f"切on相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 检查是否有切off按钮
def check_qie_off():
    # 检查参考图片是否存在
    reference_path = "referenceImg/qie_off.jpg"
    if not os.path.exists(reference_path):
        print("错误：同目录下未找到切off.jpg文件")
        return

    # 加载参考图片
    reference_img = cv2.imread(reference_path)

    # 截取屏幕（会自动保存）
    screen_img = mjtools.capture(qie_button_position)

    # 比较相似度
    similarity = mjtools.compare_images_with_color(screen_img, reference_img)
    # print(f"切off相似度: {similarity:.2f}%")
    if similarity >= 90:
        return True
    else:
        return False


# 点击 再来一场 按钮
def click_next_game():
    x = (next_game_button_position.left + next_game_button_position.right) / 2
    y = (next_game_button_position.top + next_game_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击 再来一场的确认 按钮
def click_next_game_confirm():
    x = (next_game_confirm_button_position.left + next_game_confirm_button_position.right) / 2
    y = (next_game_confirm_button_position.top + next_game_confirm_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


def click_ranking():
    x = (ranking_position.left + ranking_position.right) / 2
    y = (ranking_position.top + ranking_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


def click_silver_room():
    x = (silver_room_position.left + silver_room_position.right) / 2
    y = (silver_room_position.top + silver_room_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


def click_siren_dong():
    x = (siren_dong_position.left + siren_dong_position.right) / 2
    y = (siren_dong_position.top + siren_dong_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击 确认 按钮
def click_confirm():
    x = (confirm_button_position.left + confirm_button_position.right) / 2
    y = (confirm_button_position.top + confirm_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击 确认 按钮
def click_get_reward():
    x = (get_reward_position.left + get_reward_position.right) / 2
    y = (get_reward_position.top + get_reward_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


def click_end_confirm():
    x = (end_confirm_button_position.left + end_confirm_button_position.right) / 2
    y = (end_confirm_button_position.top + end_confirm_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击 第一 按钮
def click_first_button():
    x = (first_button_position.left + first_button_position.right) / 2
    y = (first_button_position.top + first_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击 第二 按钮
def click_second_button():
    x = (second_button_position.left + second_button_position.right) / 2
    y = (second_button_position.top + second_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击 第三 按钮
def click_third_button():
    x = (third_button_position.left + third_button_position.right) / 2
    y = (third_button_position.top + third_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击 第一 按钮
def click_pos_button(pos: int):
    if pos == 1:
        click_first_button()
    elif pos == 2:
        click_second_button()
    elif pos == 3:
        click_third_button()


# 点击 左侧 和 按钮
def click_he():
    x = (he_button_position.left + he_button_position.right) / 2
    y = (he_button_position.top + he_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击 左侧 鸣 按钮
def click_ming():
    x = (ming_button_position.left + ming_button_position.right) / 2
    y = (ming_button_position.top + ming_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)


# 点击 左侧 切 按钮
def click_qie():
    x = (qie_button_position.left + qie_button_position.right) / 2
    y = (qie_button_position.top + qie_button_position.bottom) / 2
    mjtools.move_to_and_click(x, y)

# 计算右下角区域的坐标
# 确认按钮位置
# left = screen_width - 300
# top = screen_height - 125
# right = screen_width - 50
# bottom = screen_height - 55
# 再来一场按钮位置
# left = screen_width - 585
# top = screen_height - 125
# right = screen_width - 335
# bottom = screen_height - 55
# 和按钮位置
# left = 15
# top = 590
# right = 65
# bottom = 640
# 鸣按钮位置
# left = 15
# top = 653
# right = 65
# bottom = 703
# 切按钮位置
# left = 15
# top = 716
# right = 65
# bottom = 766
# 牌位置（不含新牌）
# left = 220
# top = screen_height - 160
# right = screen_width - 465
# bottom = screen_height - 10
# 牌位置（含新牌）
# left = 220
# top = screen_height - 160
# right = screen_width - 340
# bottom = screen_height - 10
# 跳过按钮位置
# left = screen_width - 700
# top = screen_height - 270
# right = screen_width - 530
# bottom = screen_height - 217
# 按钮二位置
# left = screen_width - 990
# top = screen_height - 270
# right = screen_width - 820
# bottom = screen_height - 217
# 按钮三位置
# left = screen_width - 1280
# top = screen_height - 270
# right = screen_width - 1110
# bottom = screen_height - 217
